package cn.hx.admin.aop;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.Date;

@Component
@Aspect
public class AllAspect {
    private static final String CREATE_BY = "createBy";
    private static final String CREATE_TIME = "createTime";
    private static final String UPDATE_BY = "updateBy";
    private static final String UPDATE_TIME = "updateTime";

  @Around("execution(* cn.hx.admin.service.impl.*.insert(..))")
    public Object setCreateTime(ProceedingJoinPoint joinPoint) throws Throwable {
        String userId = "admin";
        Object[] args = joinPoint.getArgs();
        Object arg = args[0];
        Class<?> clazz = arg.getClass();
        try {
            Field createTime = clazz.getDeclaredField(CREATE_TIME);
            if (createTime != null) {
                createTime.setAccessible(true);
                createTime.set(arg, new Date());
            }
        } catch (Exception e) {
        }
        try {
            Field updateTime = clazz.getDeclaredField(UPDATE_TIME);
            if (updateTime != null) {
                updateTime.setAccessible(true);
                updateTime.set(arg, new Date());
            }
        } catch (Exception e) {

        }
        try {
            Field createUser = clazz.getDeclaredField(CREATE_BY);
            if (createUser != null) {
                // 将来改为修改为登录的用户
                createUser.setAccessible(true);
                createUser.set(arg, userId);
            }
        } catch (Exception e) {

        }
        try {
            Field updateUser = clazz.getDeclaredField(UPDATE_BY);
            if (updateUser != null) {
                updateUser.setAccessible(true);
                updateUser.set(arg, userId);
            }
        } catch (Exception e) {
        }
        Object proceed = joinPoint.proceed();
        return proceed;
    }

   @Around("execution(* cn.hx.admin.service.impl.*.update(..))")
    public Object setUpdateTime(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        Object arg = args[0];
        Class<?> clazz = arg.getClass();
        try {
            Field updateTime = clazz.getDeclaredField(UPDATE_TIME);
            if (updateTime != null) {
                updateTime.setAccessible(true);
                updateTime.set(arg, new Date());
            }
        } catch (NoSuchFieldException e) {
        }

        Object proceed = joinPoint.proceed();
        return proceed;
    }

    // 分页AOP
    @Around("execution(* cn.hx.admin.controller.*.queryByPage(..))")
    public Object page(ProceedingJoinPoint joinPoint) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String page = request.getParameter("page");
        String size = request.getParameter("size");
        String orderBy = request.getParameter("orderBy");
        System.out.println(orderBy);
        if (StringUtils.hasText(page) && StringUtils.hasText(size)) {
            Page<Object> objects = PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(size), StringUtils.hasText(orderBy) ? orderBy : null);
            joinPoint.proceed();
            return ResponseEntity.ok(new PageInfo<>(objects));
        }
        Object proceed = joinPoint.proceed();
        System.out.println("进入aop ");
        return proceed;
    }

}
